
#include "../../inc/Ui/ShowFindLocationResultForm.h"
#include "Ui/FormManager.h"
#include "Ui/WebForm.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

int ShowFindLocationResultForm::FormId = -1;

ShowFindLocationResultForm::ShowFindLocationResultForm(void)
{
}

ShowFindLocationResultForm::~ShowFindLocationResultForm(void)
{
}

bool
ShowFindLocationResultForm::Initialize()
{
	Form::Construct(L"ShowFindLocationResultForm");

	return true;
}

result
ShowFindLocationResultForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_OK);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_CANCEL);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);

	pList = static_cast<List*>(GetControl(L"ResultPlaceList"));
	if (pList == null){
		r = E_FAILURE;
		return r;
	}
	//pList->SetColumn1Width(500);

	return r;
}

result
ShowFindLocationResultForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void ShowFindLocationResultForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs){
	if (pList->GetItemCount() > 0){
		pList->RemoveAllItems();
	}
	int count = pArgs->GetCount();
	DiaChi *pDiaChi = null;
	for (int i = 0; i < count; i++){
		pDiaChi = (DiaChi*)pArgs->GetAt(i);
		if (pDiaChi != null){
			pList->AddItem(&(pDiaChi->tenDiaDiem), null, null, null);
		//	delete item;

		}
	}
	pList->SetItemChecked(0, true);
	listItem = pArgs;

}

void ShowFindLocationResultForm::OnActionPerformed(const Control &source, int actionId){
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);

	DiaChi *pDiaChi = null;

	switch (actionId) {
		case ID_SOFTKEY_OK:

			pDiaChi = (static_cast<DiaChi*>(listItem->GetAt(pList->GetLastCheckedItemIndex())));
			pFormManager->SetCurrentForm(WebForm::GetFormId());
			WebController::SetMarkerN(pDiaChi->toaDo.lat,
										pDiaChi->toaDo.lng,
										pDiaChi->tenDiaDiem, String(L"LocationIcon.png"),true);

			listItem->RemoveAll(false);
			delete listItem;
			break;
		case ID_SOFTKEY_CANCEL:
			pFormManager->SetCurrentForm(WebForm::GetFormId());
			listItem->RemoveAll(false);
			delete listItem;

		default:
			break;
	}
}

